package xyz.molzhao.config;

import com.baomidou.dynamic.datasource.plugin.MasterSlaveAutoRoutingPlugin;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xyz.molzhao.crypt.CryptInterceptor;

/**
 * MyBatisPlus配置类
 */
@Slf4j
@Configuration
@MapperScan("xyz.molzhao.mapper")
public class MyBatisPlusCommonConfigure {

    // 读写分离组件
    @Bean
    public MasterSlaveAutoRoutingPlugin masterSlaveAutoRoutingPlugin() {
        return new MasterSlaveAutoRoutingPlugin();
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> {
            //插件拦截链采用了责任链模式，执行顺序和加入连接链的顺序有关
            CryptInterceptor myPlugin = new CryptInterceptor();
            //设置参数，比如阈值等，可以在配置文件中配置，这里直接写死便于测试
            configuration.addInterceptor(myPlugin);
        };
    }
}
